home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / Distort.frm < prev    next >
Text File  |  1999-06-17  |  8KB  |  275 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmDistort 
  4.    Caption         =   "Distort"
  5.    ClientHeight    =   4920
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   4575
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   4920
  11.    ScaleWidth      =   4575
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog dlgFile 
  14.       Left            =   3960
  15.       Top             =   0
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.    End
  20.    Begin VB.PictureBox picCanvas 
  21.       Height          =   4335
  22.       Left            =   120
  23.       ScaleHeight     =   4275
  24.       ScaleWidth      =   4275
  25.       TabIndex        =   4
  26.       Top             =   480
  27.       Width           =   4335
  28.    End
  29.    Begin VB.OptionButton optTransformation 
  30.       Caption         =   "Fish Eye"
  31.       Height          =   255
  32.       Index           =   3
  33.       Left            =   3360
  34.       TabIndex        =   3
  35.       Top             =   120
  36.       Width           =   975
  37.    End
  38.    Begin VB.OptionButton optTransformation 
  39.       Caption         =   "Twist"
  40.       Height          =   255
  41.       Index           =   2
  42.       Left            =   2280
  43.       TabIndex        =   2
  44.       Top             =   120
  45.       Width           =   975
  46.    End
  47.    Begin VB.OptionButton optTransformation 
  48.       Caption         =   "Wave"
  49.       Height          =   255
  50.       Index           =   1
  51.       Left            =   1200
  52.       TabIndex        =   1
  53.       Top             =   120
  54.       Width           =   975
  55.    End
  56.    Begin VB.OptionButton optTransformation 
  57.       Caption         =   "None"
  58.       Height          =   255
  59.       Index           =   0
  60.       Left            =   120
  61.       TabIndex        =   0
  62.       Top             =   120
  63.       Width           =   975
  64.    End
  65.    Begin VB.Menu mnuFile 
  66.       Caption         =   "&File"
  67.       Begin VB.Menu mnuFileSaveToMetafile 
  68.          Caption         =   "&Save to Metafile..."
  69.          Shortcut        =   ^S
  70.       End
  71.    End
  72. End
  73. Attribute VB_Name = "frmDistort"
  74. Attribute VB_GlobalNameSpace = False
  75. Attribute VB_Creatable = False
  76. Attribute VB_PredeclaredId = True
  77. Attribute VB_Exposed = False
  78. Option Explicit
  79.  
  80. Private Const POINTS_PER_ROW = 20
  81. Private PointX(1 To POINTS_PER_ROW, 1 To POINTS_PER_ROW) As Single
  82. Private PointY(1 To POINTS_PER_ROW, 1 To POINTS_PER_ROW) As Single
  83.  
  84. ' Matefile API functions.
  85. Private Declare Function CreateMetaFile Lib "gdi32" Alias "CreateMetaFileA" (ByVal lpString As String) As Long
  86. Private Declare Function CloseMetaFile Lib "gdi32" (ByVal hmf As Long) As Long
  87. Private Declare Function DeleteMetaFile Lib "gdi32" (ByVal hmf As Long) As Long
  88. Private Declare Function SetWindowExtEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE) As Long
  89. Private Type SIZE
  90.     Cx As Long
  91.     Cy As Long
  92. End Type
  93. Private Declare Function MoveTo Lib "gdi32" Alias "MoveToEx" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal lpPoint As Long) As Long
  94. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
  95.  
  96. ' Draw the transformed points.
  97. Private Sub DrawPoints(ByVal pic As PictureBox)
  98. Dim i As Integer
  99. Dim j As Integer
  100.  
  101.     ' Draw the horizontal lines.
  102.     For i = 1 To POINTS_PER_ROW
  103.         pic.CurrentX = PointX(i, 1)
  104.         pic.CurrentY = PointY(i, 1)
  105.         For j = 2 To POINTS_PER_ROW
  106.             pic.Line -(PointX(i, j), PointY(i, j))
  107.         Next j
  108.     Next i
  109.  
  110.     ' Draw the vertical lines.
  111.     For j = 1 To POINTS_PER_ROW
  112.         pic.CurrentX = PointX(1, j)
  113.         pic.CurrentY = PointY(1, j)
  114.         For i = 2 To POINTS_PER_ROW
  115.             pic.Line -(PointX(i, j), PointY(i, j))
  116.         Next i
  117.     Next j
  118. End Sub
  119.  
  120. ' Draw the transformed points into a metafile.
  121. Private Sub DrawPointsIntoMetafile(ByVal pic As PictureBox, ByVal mf_dc As Long)
  122. Dim i As Integer
  123. Dim j As Integer
  124.  
  125.     ' Draw the horizontal lines.
  126.     For i = 1 To POINTS_PER_ROW
  127.         MoveTo mf_dc, PointX(i, 1), PointY(i, 1), ByVal 0&
  128.         For j = 2 To POINTS_PER_ROW
  129.             LineTo mf_dc, PointX(i, j), PointY(i, j)
  130.         Next j
  131.     Next i
  132.  
  133.     ' Draw the vertical lines.
  134.     For j = 1 To POINTS_PER_ROW
  135.         MoveTo mf_dc, PointX(1, j), PointY(1, j), ByVal 0&
  136.         For i = 2 To POINTS_PER_ROW
  137.             LineTo mf_dc, PointX(i, j), PointY(i, j)
  138.         Next i
  139.     Next j
  140. End Sub
  141.  
  142.  
  143. ' Create the data points.
  144. Private Sub MakeData(ByVal pic As PictureBox)
  145. Const SQUARES_MARGIN = 2
  146. Dim dx As Single
  147. Dim dy As Single
  148. Dim X As Single
  149. Dim Y As Single
  150. Dim i As Integer
  151. Dim j As Integer
  152.  
  153.     dx = pic.ScaleWidth / (POINTS_PER_ROW + 2 * SQUARES_MARGIN - 1)
  154.     dy = pic.ScaleHeight / (POINTS_PER_ROW + 2 * SQUARES_MARGIN - 1)
  155.     Y = pic.ScaleTop + dy * SQUARES_MARGIN
  156.     For i = 1 To POINTS_PER_ROW
  157.         X = pic.ScaleLeft + dx * SQUARES_MARGIN
  158.         For j = 1 To POINTS_PER_ROW
  159.             PointX(i, j) = X
  160.             PointY(i, j) = Y
  161.             X = X + dx
  162.         Next j
  163.         Y = Y + dy
  164.     Next i
  165. End Sub
  166.  
  167. Private Sub Form_Load()
  168.     picCanvas.ScaleMode = vbPixels
  169.  
  170.     dlgFile.InitDir = App.Path
  171.     dlgFile.Filter = "Metafiles (*.wmf)|*.wmf|" & _
  172.         "All Files (*.*)|*.*"
  173.     dlgFile.CancelError = True
  174.     dlgFile.Flags = cdlOFNExplorer Or _
  175.         cdlOFNLongNames Or _
  176.         cdlOFNHideReadOnly Or _
  177.         cdlOFNOverwritePrompt
  178.  
  179.     optTransformation(0).Value = True
  180. End Sub
  181.  
  182. Private Sub mnuFileSaveToMetafile_Click()
  183. Dim file_name As String
  184. Dim mf_dc As Long
  185. Dim hmf As Long
  186. Dim old_size As SIZE
  187.  
  188.     On Error Resume Next
  189.     dlgFile.ShowSave
  190.     If Err.Number = cdlCancel Then
  191.         Exit Sub
  192.     ElseIf Err.Number <> 0 Then
  193.         MsgBox "Error " & Format$(Err.Number) & _
  194.             " selecting file." & vbCrLf & _
  195.                 Err.Description
  196.     End If
  197.     On Error GoTo 0
  198.  
  199.     ' Get the file name.
  200.     file_name = dlgFile.FileName
  201.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  202.         - Len(dlgFile.FileTitle) - 1)
  203.  
  204.     ' Create the metafile.
  205.     mf_dc = CreateMetaFile(ByVal file_name)
  206.     If mf_dc = 0 Then
  207.         MsgBox "Error creating the metafile.", vbExclamation
  208.         Exit Sub
  209.     End If
  210.  
  211.     ' Set the metafile's size to something reasonable.
  212.     SetWindowExtEx mf_dc, picCanvas.ScaleWidth, picCanvas.ScaleHeight, old_size
  213.  
  214.     ' Draw into the metafile.
  215.     DrawPointsIntoMetafile picCanvas, mf_dc
  216.  
  217.     ' Close the metafile.
  218.     hmf = CloseMetaFile(mf_dc)
  219.     If hmf = 0 Then
  220.         MsgBox "Error closing the metafile.", vbExclamation
  221.     End If
  222.  
  223.     ' Delete the metafile to free resources.
  224.     If DeleteMetaFile(hmf) = 0 Then
  225.         MsgBox "Error deleting the metafile.", vbExclamation
  226.     End If
  227. End Sub
  228.  
  229. Private Sub optTransformation_Click(Index As Integer)
  230. Dim obj As Transformation
  231. Dim twist As TransTwist
  232. Dim wave As TransWave
  233. Dim fish As TransFish
  234. Dim i As Integer
  235. Dim j As Integer
  236.  
  237.     ' Make the data.
  238.     MakeData picCanvas
  239.  
  240.     ' Get the transformation object.
  241.     If optTransformation(1).Value Then
  242.         Set wave = New TransWave
  243.         wave.Amplitude = 8
  244.         wave.Period = 20 * 8
  245.         Set obj = wave
  246.     ElseIf optTransformation(2).Value Then
  247.         Set twist = New TransTwist
  248.         twist.Cx = picCanvas.ScaleWidth / 2
  249.         twist.Cy = picCanvas.ScaleHeight / 2
  250.         twist.TwistSpeed = 20
  251.         Set obj = twist
  252.     ElseIf optTransformation(3).Value Then
  253.         Set fish = New TransFish
  254.         fish.Cx = picCanvas.ScaleWidth / 2
  255.         fish.Cy = picCanvas.ScaleHeight / 2
  256.         fish.Radius = picCanvas.ScaleWidth
  257.         Set obj = fish
  258.     Else
  259.         Set obj = New TransIdentity
  260.     End If
  261.  
  262.     ' Transform the points.
  263.     For i = 1 To POINTS_PER_ROW
  264.         For j = 1 To POINTS_PER_ROW
  265.             obj.Transform PointX(i, j), PointY(i, j)
  266.         Next j
  267.     Next i
  268.  
  269.     ' Redraw.
  270.     picCanvas.Refresh
  271. End Sub
  272. Private Sub picCanvas_Paint()
  273.     DrawPoints picCanvas
  274. End Sub
  275.